#include "app_common.h"
#include "sys/time.h"
#include "stddef.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"

long app_common_getCurrentTime(void)
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    // tv_sec秒、tv_usec微秒 相加转为毫秒ms
    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

char *app_common_hexStr2Chars(char *hexStr, int *chars_len)
{
    // 16进制字符串转字符数组
    // 获取字符串长度
    int hexStr_len = strlen(hexStr);
    // 申请内存
    char *chars_buf = (char *)malloc(hexStr_len / 2);
    for (int i = 0; i < hexStr_len; i += 2)
    {
        sscanf(hexStr + i, "%2hhx", chars_buf + i / 2);
    }
    *chars_len = hexStr_len / 2;
    return chars_buf;
}

char *app_common_chars2Hex(char *chars, int chars_len)
{
    // 字符数组转16进制字符串
    // 申请内存
    char *hexStr = (char *)malloc(chars_len * 2 + 1);
    // 遍历
    for (int i = 0; i < chars_len; i++)
    {
        sprintf(hexStr + i * 2, "%02x", chars[i]);
    }
    return hexStr;
}
